package com.example.task1.ui.AllService.pet;

import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.task1.R;
import com.example.task1.application.MyApplication;
import com.example.task1.bean.PetAskListBean;
import com.example.task1.bean.PetSortBean;
import com.example.task1.http.Api;
import com.example.task1.util.ApiResp;
import com.example.task1.util.CallBack;

import retrofit2.Call;

public class PetActivity extends AppCompatActivity {

    private RecyclerView rvPet;
    private RecyclerView rvAsk;
    private Button btnMyList;
    private int pageNum = 1;
    private LinearLayoutManager linearLayoutManager;
    private PetAskListAdapter petAskListAdapter;
    private boolean isTrue = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pet);
        ActionBar supportActionBar = getSupportActionBar();
        supportActionBar.setTitle("宠物医院");
        supportActionBar.setDisplayHomeAsUpEnabled(true);
        initView();
        initData();
        setAdapter();
        getAskList();
        loadCommentList();
        btnMyList.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyApplication.intent(PetActivity.this, MyAskActivity.class);
            }
        });
    }

    private void loadCommentList() {
        rvAsk.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                if (petAskListAdapter.getItemCount() - 1 == linearLayoutManager.findLastVisibleItemPosition() && isTrue) {
                    isTrue = false;
                    getAskList();
                }
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

    private void setAdapter() {
        petAskListAdapter = new PetAskListAdapter(this);
        linearLayoutManager = new LinearLayoutManager(this);
        rvAsk.setAdapter(petAskListAdapter);
        rvAsk.setLayoutManager(linearLayoutManager);
    }


    private void getAskList() {
        Call<ApiResp> askCall = Api.api().get(MyApplication.TOKEN, "/prod-api/api/pet-hospital/inquiry/list?pageNum=" + pageNum + "&pageSize=10");
        Api.send(askCall, PetAskListBean.class, new CallBack<PetAskListBean>() {
            @Override
            public void success(PetAskListBean data) {
                if (petAskListAdapter.getData().size() < data.getTotal()) {
                    pageNum++;
                    isTrue = true;
                    petAskListAdapter.setData(data.getRows());
                } else {
                    MyApplication.show("没有更多数据了");
                }
            }

            @Override
            public void error(String msg) {

            }
        });
    }

    private void initData() {
        Call<ApiResp> petCall = Api.api().get(MyApplication.TOKEN, "/prod-api/api/pet-hospital/pet-type/list");
        Api.send(petCall, PetSortBean.class, new CallBack<PetSortBean>() {
            @Override
            public void success(PetSortBean data) {
                PetSortAdapter petSortAdapter = new PetSortAdapter(PetActivity.this);
                GridLayoutManager gridLayoutManager = new GridLayoutManager(PetActivity.this, 5);
                rvPet.setLayoutManager(gridLayoutManager);
                petSortAdapter.setData(data.getData());
                rvPet.setAdapter(petSortAdapter);
            }

            @Override
            public void error(String msg) {

            }
        });
    }

    private void initView() {
        rvPet = (RecyclerView) findViewById(R.id.rv_pet);
        rvAsk = (RecyclerView) findViewById(R.id.rv_ask);
        btnMyList = (Button) findViewById(R.id.btn_myList);
    }
}